package foard.model
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import foard.core.Settings;
	
	import mx.collections.ArrayCollection;
	
	public class BoardModel extends ArrayCollection
	{
		protected var array:Array;
		protected var loader:URLLoader;
		
		public function BoardModel(data:String)
		{
			array = new Array();
			super(array);
			
			setData(data);
		}
		
		//--
		
		private var _data:String;
		
		public function get data():String
		{
			return _data;
		}
		
		private var _title:String;
		
		public function get title():String
		{
			return _title;
		}
		
		protected var _path:String;
		
		public function get path():String
		{
			return _path;
		}
		
		public function get subjectPath():String
		{
			return Settings.PROXY_PATH+path+"subject.txt";
		}
		
		//--
		
		public function setData(data:String):void
		{
			_data = data;
			_path = data.substring(data.indexOf("HREF=")+5, data.indexOf(">"));
			if(_path.indexOf("TARGET=") > 0)
			{
				_path = _path.substring(0, _path.indexOf("TARGET=")-1);
			}
			_title = data.substring(data.indexOf(">")+1, data.indexOf("</A>"));
		}
		
		public function load():void
		{
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
			loader.load(new URLRequest(subjectPath));
		}
		
		protected function loaderCompleteHandler(e:Event):void
		{
			var data:String = loader.data;
			var arr:Array = data.split("\n");
			
			for each(var t:String in arr)
			{
				if(t == "")
				{
					continue;
				}
				
				var m:ThreadModel = new ThreadModel(t, this);
				if(m.length)
				{
					array.push(m);
				}
			}
			
			refresh();
		}
		
		public override function toString():String
		{
			return _title+" - "+_path;
		}
	}
}









